//
//  ZZCCreditController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/23.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCCreditController.h"
#import "ZZCCreditFailController.h"

@interface ZZCCreditController ()<UITextFieldDelegate>
//用户
@property (weak,   nonatomic) IBOutlet UITextField *userTextField;
//用途
@property (weak,   nonatomic) IBOutlet UITextField *useTextField;
//联系方式
@property (weak,   nonatomic) IBOutlet UITextField *contactTextField;
//申请金额
@property (weak,   nonatomic) IBOutlet UITextField *moneyTextField;
//银行卡号
@property (weak,   nonatomic) IBOutlet UITextField *bankTextField;
//身份证号
@property (weak,   nonatomic) IBOutlet UITextField *cardTextField;
@property (strong, nonatomic) ZZCMenuView *menuViewA;
@property (strong, nonatomic) ZZCMenuView *menuViewB;
@property (weak,   nonatomic) IBOutlet UIButton *guaranteeBtn;
@property (weak, nonatomic) IBOutlet UIButton *cycleBtn;

@property (weak,   nonatomic) IBOutlet UIView *guaranteeView;
@property (assign, nonatomic) NSInteger is_company;

@property (retain, nonatomic) NSArray *array;
@property (retain, nonatomic) NSMutableArray *cycleArr;
@property (retain, nonatomic) NSMutableArray *rateArr;
@property (retain, nonatomic) NSMutableArray *iidArr;
@property (assign,   nonatomic) float rate;
@property (assign,   nonatomic) int iid;
@end

@implementation ZZCCreditController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"申请授信";
    [self requestData];
    _iidArr = [NSMutableArray new];
    _cycleArr = [NSMutableArray new];
    _rateArr = [NSMutableArray new];
    self.contactTextField.delegate = self;
    self.cardTextField.delegate = self;
    self.bankTextField.delegate = self;
    self.contactTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.cardTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.bankTextField.keyboardType = UIKeyboardTypeNumberPad;

}

- (void)requestData {
//    NSString * = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"type":@(1)};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,
                            @"type":@(1)};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_RATE parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"成功-%@",responseObject);
            _array = responseObject[@"data"][@"list"];
            for (NSDictionary *dict in _array) {
                [_cycleArr addObject:[NSString stringWithFormat:@"%@个月",dict[@"cycle"]]];
                [_iidArr addObject:dict[@"id"]];
                 [_rateArr addObject:dict[@"rate"]];
            }
        }
    }];
}


//担保方式
- (IBAction)guaranteeBtnClick {
    ZZCLog(@"选择担保方式");
    NSArray *arr = @[@"抵押"];
    [self.menuViewA removeFromSuperview];
    self.menuViewA= [[ZZCMenuView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.guaranteeView.frame), ZZCScreenW, 132) contentArray:arr];
    __weak typeof (ZZCMenuView *) weakSelf = self.menuViewA;
    __weak typeof (self) weakself = self;
    self.menuViewA.block = ^(BOOL isSeletct ,NSString *title){
        
        [weakSelf removeFromSuperview];
        if (title.length != 0) {
            [weakself.guaranteeBtn setTitle:title forState:0];
            if ([title isEqualToString:@"抵押"]) {
                weakself.is_company = 1;
            }
        }
    };
    
    [self.view addSubview:self.menuViewA];
}
- (IBAction)cycleBtnAction:(UIButton *)sender {
    ZZCLog(@"选择选择周期");
    [self.menuViewB removeFromSuperview];
    self.menuViewB= [[ZZCMenuView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.guaranteeView.frame), ZZCScreenW, 132) contentArray:_cycleArr];
    __weak typeof (ZZCMenuView *) weakSelf = self.menuViewB;
    __weak typeof (self) weakself = self;
    self.menuViewB.block = ^(BOOL isSeletct ,NSString *title){
        
        [weakSelf removeFromSuperview];
        if (title.length != 0) {
            [weakself.cycleBtn setTitle:title forState:0];
            NSInteger Index  = [weakself.cycleArr indexOfObject:title];
            weakself.rate = [weakself.rateArr[Index] floatValue];
            weakself.iid = [weakself.iidArr[Index] floatValue];

        }
    };
    
    [self.view addSubview:self.menuViewB];

}

//提交
- (IBAction)submitClick {
    ZZCLog(@"提交");
    [self requestCreditData];
}

- (void)requestCreditData {
    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"token":token,
                           @"username":self.userTextField.text,
                           @"use":self.useTextField.text,
                           @"phone":self.contactTextField.text,
                           @"money":self.moneyTextField.text,
                           @"bank_card":self.bankTextField.text,
                           @"id_card":self.cardTextField.text,
                           @"type":@(self.is_company),
                           @"rate_id":@(self.iid)
                           };
    NSLog(@"%@",dict);
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,
                            @"token":token,
                            @"username":self.userTextField.text,
                            @"use":self.useTextField.text,
                            @"phone":self.contactTextField.text,
                            @"money":self.moneyTextField.text,
                            @"bank_card":self.bankTextField.text,
                            @"id_card":self.cardTextField.text,
                            @"type":@(self.is_company),
                            @"rate_id":@(self.iid)
};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_MYQUOTA parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            return;
        }else {
            ZZCLog(@"成功-%@",responseObject);
            //在此做判断是否申请成功
//            if (YES) {
//                ZZCLog(@"提交失败");
//                ZZCCreditFailController *credit = [ZZCCreditFailController new];
//            } else {
//                ZZCLog(@"提交成功");
//            }
            if ([responseObject[@"code"] isEqual: @(400)]) {
                [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                return;
            } else if([responseObject[@"code"] isEqual: @(200)]){
                [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                //这里可以做回到登录界面的一些数据操作
                [[NSNotificationCenter defaultCenter] postNotificationName:@"creditSucceed" object:nil];

                [self.navigationController popViewControllerAnimated:YES];
            }

        }
    }];
}

- (void)removeObjectFromRateArrAtIndex:(NSUInteger)index {
    
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{
    if (textField == self.contactTextField) {
        //用来做长度限制；
        if (range.location>=11) {
            return NO;
        }
    }
    if (textField == self.cardTextField) {
        //用来做长度限制；
        if (range.location>=18) {
            return NO;
        }
    }
    return YES;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
